using Microsoft.AspNetCore.Mvc;

namespace HttpContextBodyToStreamContentDemo.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class OrderController : ControllerBase
{
    private readonly IHttpClientFactory _httpClientFactory;

    public OrderController(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    [HttpPost]
    public async Task<ActionResult> CreateAsync(CreateOrderDto input)
    {
        using var httpClient = _httpClientFactory.CreateClient();
        var stream = HttpContext.Request.BodyReader.AsStream();
        var streamContent = new StreamContent(stream);
        var httpResponseMessage = await httpClient.PostAsync("https://localhost:7132/Order", streamContent);

        //var stringContent = new StringContent(System.Text.Json.JsonSerializer.Serialize(input), null, "application/json");
        //var httpResponseMessage = await httpClient.PostAsync("https://localhost:7132/Order", stringContent);

        HttpContext.Response.StatusCode = (int)httpResponseMessage.StatusCode;
        var content = await httpResponseMessage.Content.ReadAsStringAsync();
        return Content(content, "application/json");
    }

    public class CreateOrderDto
    {
        public string Name { get; set; } = "test";
    }
}
